<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2019 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://demo.thinkadmin.top
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/zoujingli/ThinkAdmin
// | github 代码仓库：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\store\controller;

use library\Controller;
use library\tools\Data;
use think\Db;
use app\store\model\Common;
/**
 * 合作申请列表
 * Class Documentlist
 * @package app\store\controller
 */
class Cooperate extends Controller
{
    /**
     * 指定数据表
     * @var string
     */
    protected $table = 'StoreMemberCooperate';
    protected $table1 = 'store_member_cooperate';

    /**
     * 合作申请列表
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '合作申请列表';
        $this->user_list = Db::table("store_member")->field("id,username")->select();

        $query = $this->_query($this->table)->like('username,phone')->equal("user_id,status");
        $query->order('id desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        foreach ($data as &$vo) {
            $u_id = $vo['user_id'];
            $vo['user'] = Db::table("store_member")->where("id=$u_id")->find();

            if($vo['status']==0){  $vo['status_r'] = '<span style="color: orangered;font-weight: bold">未同意合作</span>'; }
            if($vo['status']==1){  $vo['status_r'] = '<span style="color: green;font-weight: bold">同意合作</span>'; }
            if($vo['status']==2){  $vo['status_r'] = '<span style="color: red">拒绝合作</span>'; }

        }
    }

    /**
     * 启用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function resume()
    {
        $find = Db::table("store_member_cooperate")->where(['id'=>$_REQUEST['id']])->find();
        if($find){
            Db::table("store_member")->where(['id'=>$find['user_id']])->update(['is_admin'=>1]);
        }
        $this->_save("store_member_cooperate", ['status' => '1','audit_at'=>date('Y-m-d H:i:s')]);
    }

    /**
     * 禁用
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function forbid()
    {
        $this->_save("store_member_cooperate", ['status' => '2','audit_at'=>date('Y-m-d H:i:s')]);
    }

    /**
     * 修改状态
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit_ziduan(){
        $id = $_POST['id']??'';
        $ziduan = $_POST['ziduan']??'';
        $val = $_POST['val']??'';
        Db::query("update store_member_cooperate set $ziduan='$val' where id=$id");

        $find = Db::table("store_member_cooperate")->where("id=$id")->find();
        $user_id = $find['user_id'];
        Db::query("update store_power_station set $ziduan='$val' where user_id=$user_id");
    }

}
